package com.baiyun.controller.admin;

import com.baiyun.Result.Result;
import com.baiyun.entity.Tea;
import com.baiyun.service.TeaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@Api(tags = "管理员端——茶叶管理")
@RequestMapping("/admin/teas")
@RequiredArgsConstructor
public class TeaController {

    private final TeaService teaService;

    /**
     * 查询所有茶叶信息
     *
     * @return
     */
    @GetMapping()
    @ApiOperation(value = "查询所有茶叶信息")
    public Result<List<Tea>> selectAllTeas() {
        log.info("查询所有茶叶信息");
        try {
            return Result.success(teaService.selectAllTeas());
        } catch (Exception e) {
            return Result.error("查询失败");
        }
    }

    /**
     * 新增茶叶
     *
     * @param tea
     * @return
     */
    @PostMapping()
    @ApiOperation(value = "新增茶叶")
    public Result addTea(@RequestBody Tea tea) {
        log.info("新增茶叶:{}", tea);
        try {
            teaService.addTea(tea);
        } catch (Exception e) {
            return Result.error("新增失败");
        }
        return Result.success();
    }

    /**
     * 更新茶叶
     *
     * @param tea
     * @return
     */
    @PutMapping()
    @ApiOperation(value = "更新茶叶")
    public Result updateTea(@RequestBody Tea tea) {
        log.info("更新茶叶:{}", tea);
        try {
            teaService.updateTea(tea);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("更新失败");
        }
        return Result.success();
    }

    /**
     * 删除茶叶
     *
     * @param tid
     * @return
     */
    @DeleteMapping("/{tid}")
    @ApiOperation(value = "删除茶叶")
    public Result deleteTea(@ApiParam("茶叶id")@PathVariable("tid") Integer tid) {
        log.info("删除茶叶:{}", tid);
        try {
            teaService.deleteTea(tid);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error("删除失败");
    }

    /**
     * 根据id查询茶叶
     * @return
     */
    @GetMapping("/{tid}")
    @ApiOperation(value = "根据id查询茶叶")
    public Result<Tea> selectTeaType(@ApiParam("茶叶id")@PathVariable("tid") Integer tid) {
        log.info("根据id查询茶叶:{}", tid);
        try {
            return Result.success(teaService.selectTeaByID(tid));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error("查询失败");
    }

    /**
     * 根据名字查询茶叶
     * @param name
     * @return
     */
    @GetMapping("/search/{name}")
    @ApiOperation(value = "根据名字查询茶叶")
    public Result<List<Tea>> selectTeasByName(@ApiParam("茶叶名字")@PathVariable(name = "name") String name){
        try {
            List<Tea> teaList=teaService.selectTeasByName(name);
            return Result.success(teaList);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查询失败");
        }
    }

    //TODO 上传图片接口
}
